//
//  xsBossIPChangeLevelView.m
//  digital_robot_demo
//
//  Created by xskj on 2024/9/27.
//  调整层级

#import "xsBossIPChangeLevelView.h"

@interface xsBossIPChangeLevelView()

@property (nonatomic,assign)xsEditeDigitalRobotType xsRobotType;

@property (nonatomic, strong) UIView *bgView;
@property (nonatomic, strong) UIView *bottomView;

@property (nonatomic, strong) UILabel *topTitleLabel;
@property (nonatomic, strong) UIButton *xsXbutton;// x按钮

@end

@implementation xsBossIPChangeLevelView

- (void)bsChangeType:(xsEditeDigitalRobotType)RobotTyp{
    self.xsRobotType = RobotTyp;
}

- (instancetype)initWithType:(xsEditeDigitalRobotType)RobotTyp{
    
    if (self = [super init]) {
        self.backgroundColor = UIColor.blackColor;
        self.xsRobotType = RobotTyp;
     
        
        [self addSubview:self.bgView];
        [self.bgView addSubview:self.topTitleLabel];
        [self.bgView addSubview:self.xsXbutton];
        [self.bgView addSubview:self.bottomView];
        
    }
    
    return self;
}
-(void)layoutSubviews{
    [super layoutSubviews];
    //_recorder.preViewLayer.frame = self.frame;
    
    NSLog(@"layoutSubviews--self.bounds==%@",NSStringFromCGRect(self.bounds));
    
    XSLogMethod2(@"layoutSubviews")
}


- (void)updateConstraints{
    [super updateConstraints];
    //
    //    __weak __typeof (&*self)weakSelf = self;
    
    NSLog(@"updateConstraints");
    
    XSLogMethod2(@"updateConstraints")
    
}

- (void)xsChangeFrame{
    
    CGFloat margin15 = 15;
    CGFloat subViewW = self.width-2.0*margin15;
    
//    self.bgView.backgroundColor = UIColor.blackColor;
    
    [self.bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.mas_left).offset(margin15);
        make.top.bottom.equalTo(self);//.offset(10);
        make.right.equalTo(self.mas_right).offset(-margin15);
    }];
    
    
    [self.xsXbutton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgView.mas_top).offset(7.5);
        make.right.equalTo(self.bgView.mas_right);
        make.size.mas_equalTo(CGSizeMake(35, 35));
    }];
    
    [self.topTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.bgView);//.offset(10);
        make.top.equalTo(self.bgView.mas_top);
        make.height.mas_equalTo(50);
    }];
    
    [self.bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.bottom.right.equalTo(self.bgView);//.offset(margin15);
        make.top.equalTo(self.topTitleLabel.mas_bottom);//.offset(10);
    }];
    [self createFourButton];
}

- (void)createFourButton{
    
    CGFloat bottomH = self.height-50;
    CGFloat bottomW = self.width;
    CGFloat margin15 = 15.0;
    
    CGFloat buttonItemW = (bottomW-margin15*5.0)*0.25;
    CGFloat buttonItemH = buttonItemW;//
    CGFloat buttonItemY = (bottomH-buttonItemH)*0.5;
    
    NSArray *titleArr = @[kLocalizedString(@"上移"),
                          kLocalizedString(@"移到最上"),
                          kLocalizedString(@"下移"),
                          kLocalizedString(@"移到最下"),];
    
    NSArray *imageArr = @[@"bossIPShangYi",@"bossIPYiZhiZuiShang",
                          @"bossIPXiaYi",@"bossIPYiZhiZuiXia"];
    
//    for(int i=0;i<titleArr.count;i++){
//        NSString *titleStr = titleArr[i];
//        
//    }
    
    UIFont *btnFont = [UIFont systemFontOfSize:16];
    

    
  
    UIButton *lastButton;
    // 添加按钮到滚动视图
    for (int i = 0; i < titleArr.count; i++) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        
        NSString *buttonTitleStr = titleArr[i];
        
        button.frame = CGRectMake(i *(buttonItemW+margin15), buttonItemY, buttonItemW, buttonItemH);
        
//313032
        button.backgroundColor =  COLOR_HEX(0x313032);//UIColor.clearColor;//xrandomColor;//xrandomColor;
//        [button setTitle:self.buttonTitle[i] forState:UIControlStateNormal];
//        [button setTitleColor:UIColor.whiteColor forState:UIControlStateNormal];
        button.font = btnFont;
        [button addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
        [button setTag:i];
        [self.bottomView addSubview:button];
        button.layer.cornerRadius = 6.0;
        button.layer.masksToBounds = YES;
        
        CGFloat buttonImageViewWH = buttonItemW *0.6;
        CGFloat buttonImageViewX = (buttonItemW-buttonImageViewWH)*0.5;
        CGFloat buttonImageViewY = 3.0;
        
        
        NSString *buttonImageViewStr = imageArr[i];
        UIImage *inggg =  [UIImage imageNamed:buttonImageViewStr];
        UIImageView *buttonImageView = [[UIImageView alloc] init];
        buttonImageView.backgroundColor  =  UIColor.clearColor;//xrandomColor;//
        buttonImageView.userInteractionEnabled = NO;
        [button addSubview:buttonImageView];
        buttonImageView.frame = CGRectMake(buttonImageViewX, buttonImageViewY, buttonImageViewWH, buttonImageViewWH);
        buttonImageView.image = inggg;
        
        CGFloat buttonTitleLabelW = buttonItemW;
        CGFloat buttonTitleLabelX = 0;
        CGFloat buttonTitleLabelY = CGRectGetMaxY(buttonImageView.frame);//+3;
        CGFloat buttonTitleLabelH = buttonItemH-buttonTitleLabelY;//-3;
        UILabel *buttonTitleLabel = [[UILabel alloc] init];
        buttonTitleLabel.backgroundColor = UIColor.clearColor;//xrandomColor;//
        buttonTitleLabel.font = [UIFont systemFontOfSize:15];
        buttonTitleLabel.textColor = UIColor.whiteColor;
        buttonTitleLabel.numberOfLines = 1;
        buttonTitleLabel.text = buttonTitleStr;
        buttonTitleLabel.textAlignment = NSTextAlignmentCenter;
        [button addSubview:buttonTitleLabel];
        buttonTitleLabel.frame = CGRectMake(buttonTitleLabelX, buttonTitleLabelY, buttonTitleLabelW, buttonTitleLabelH);
        
        
        lastButton = button;
    }
}

- (void)buttonClicked:(UIButton *)sender{
    
    NSLog(@"buttonClicked");
    if([self.delegate respondsToSelector:@selector(xsBossIPChangeLevelButtonItemBeClick:WithType:)]){
        [self.delegate xsBossIPChangeLevelButtonItemBeClick:sender WithType:self.xsRobotType];
    }
}


- (UIView *)bgView{
    
    if(!_bgView){
        _bgView = [[UIView alloc] init];
        _bgView.backgroundColor = UIColor.clearColor;
    }
    return _bgView;
}

- (UIView *)bottomView{
    
    if(!_bottomView){
        _bottomView = [[UIView alloc] init];
        _bottomView.backgroundColor = UIColor.clearColor;
    }
    return _bottomView;
}


- (UIButton *)xsXbutton{
    if (!_xsXbutton) {
        _xsXbutton = [UIButton buttonWithType:UIButtonTypeCustom];
        _xsXbutton.backgroundColor = UIColor.clearColor;
        [_xsXbutton addTarget:self action:@selector(xsXbuttonAction:) forControlEvents:UIControlEventTouchUpInside];
        [_xsXbutton setImage:[UIImage imageNamed:@"xsBossgou"] forState:UIControlStateNormal];
    }
    return _xsXbutton;
}
- (void)xsXbuttonAction:(UIButton *)btn{
    NSLog(@"xsXbuttonAction\n\n");
   
    if([self.delegate respondsToSelector:@selector(xsBossIPChangeLevelBackbuttonEvent:)]){
        [self.delegate xsBossIPChangeLevelBackbuttonEvent:btn];
    }
}

- (UILabel *)topTitleLabel {
    if (!_topTitleLabel) {
        _topTitleLabel = [[UILabel alloc] init];
        _topTitleLabel.backgroundColor = UIColor.clearColor;
        _topTitleLabel.font = FontMedium(18);// FontSemibold(17);
        _topTitleLabel.textColor = UIColor.whiteColor;//COLOR_TextGray9B;
        _topTitleLabel.numberOfLines = 1;
        _topTitleLabel.text = @"层级";
        _topTitleLabel.textAlignment = NSTextAlignmentCenter;
    }
    return _topTitleLabel;
}

@end


